#include "pch.h"
#include "StraightSkeleton2D.h"

extern "C" void StraightSkeleton_Create(double* outorPolygon2DArray, size_t OutorPointsCount,
                                        double* holePolygon2DArray, size_t holePointsCount,
                                        double*& StraightSkeletonLineArray, int*& skeletonLineCount)
{
	Polygon_2 outorPolygon;
	outorPolygon.clear();
	for (size_t i = 0; i < OutorPointsCount; i++)
	{
		outorPolygon.push_back(Point(outorPolygon2DArray[i * 2], outorPolygon2DArray[2 * i + 1]));
	}

	Polygon_2 holePolygon;
	holePolygon.clear();
	for (size_t i = 0; i < holePointsCount; i++)
	{
		holePolygon.push_back(Point(holePolygon2DArray[i * 2], holePolygon2DArray[2 * i + 1]));
	}


	Polygon_with_holes pwh(outorPolygon);
	pwh.add_hole(holePolygon);

	SsPtr ss = create_interior_straight_skeleton_2(pwh);

	skeletonLineCount = new int(ss->size_of_halfedges());

	int halfedgeCount = ss->size_of_halfedges();
	skeletonLineCount = new int(halfedgeCount);

	StraightSkeletonLineArray = new double[halfedgeCount * 4];
	int i = 0;
	for (auto edge = ss->halfedges_begin(); edge != ss->halfedges_end(); ++edge)
	{
		auto& source = edge->opposite()->vertex()->point();
		auto& target = edge->vertex()->point();
		StraightSkeletonLineArray[i] = static_cast<double>(source.x());
		StraightSkeletonLineArray[i + 1] = static_cast<double>(source.y());
		StraightSkeletonLineArray[i + 2] = static_cast<double>(target.x());
		StraightSkeletonLineArray[i + 3] = static_cast<double>(target.y());
		i = i + 4;
	}
}
